package utils.msg;

import java.util.ArrayList;
import java.util.List;










import com.sun.jna.platform.win32.COM.IUnknown;

import model.DataBase;
import model.EventFieldConditionDetail;
import model.FieldsType;
import model.Table;
import model.UserEventDetail;

public class MsgModel {
	private String sender;
	private QueryResult queryResult;
	private UserEventDetail userEventDetail;
	private List<EventFieldConditionDetail> eventFieldConditionDetailList;
	
	public MsgModel() {
	}
	
	public MsgModel(String sender, QueryResult queryResult,
			UserEventDetail userEventDetail,
			List<EventFieldConditionDetail> eventFieldConditionDetails) {
		this.sender = sender;
		this.queryResult = queryResult;
		this.userEventDetail = userEventDetail;
		this.eventFieldConditionDetailList = eventFieldConditionDetails;
	}



	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public QueryResult getQueryResult() {
		return queryResult;
	}

	public void setQueryResult(QueryResult queryResult) {
		this.queryResult = queryResult;
	}

	public UserEventDetail getUserEventDetail() {
		return userEventDetail;
	}

	public void setUserEventDetail(UserEventDetail userEventDetail) {
		this.userEventDetail = userEventDetail;
	}
	
	
	
	public List<EventFieldConditionDetail> getEventFieldConditionDetails() {
		return eventFieldConditionDetailList;
	}

	public void setEventFieldConditionDetails(
			List<EventFieldConditionDetail> eventFieldConditionDetails) {
		this.eventFieldConditionDetailList = eventFieldConditionDetails;
	}

	public String generateMsg(){
		String msg = "";
		
		//UserEvent ue = userEventDetail.getUserEvent();
		Table table = userEventDetail.getTable();
		DataBase dataBase = userEventDetail.getDataBase();
		
		
		
		String createTime = getValueByFieldType(queryResult,FieldsType.TIMESTAMP);//获得其中的TIMESTAMP类型字段值
		String dbName = dataBase.getDbName();
		String tableName = table.getTableName();
		String location = getValueByFieldType(queryResult, FieldsType.LOCATION);//获得其中的LOCATION类型字段值
		
		retainMonitorQueryResult(queryResult);
		
		String fieldString = null;
		String valueString = null;
		if(queryResult.getFieldNames().size()>1){
			fieldString = String.join(",", queryResult.getFieldNames().toArray(new String[0]));
			valueString = String.join(",", queryResult.getValues().toArray(new String[0]));
		}else {
			fieldString = queryResult.getFieldNames().get(0);
			valueString = queryResult.getValues().get(0);
		}
		
		String[] thresholdValues = getMonitorThreadsholdValue(eventFieldConditionDetailList);
		
		
		StringBuilder sb = new StringBuilder();
		sb.append(sender).append("提醒您").append(createTime).append(",").append(dbName).append("数据库-");
		sb.append(tableName).append("表-位置").append(location).append("-字段[");
		
		sb.append(fieldString).append("]的值为[").append(valueString).append("],超出预警值[");
		sb.append(String.join(",", thresholdValues)).append("]");
		msg = sb.toString();
		System.out.println("MsgModel.generateMsg(),msg="+msg);
	
		return msg;
	}
	
	/**
	 * 根据字段类型fieldsType得queryResult中的values的值
	 * @param queryResult
	 * @param fieldsType
	 * @return
	 */
	private String getValueByFieldType(QueryResult queryResult, FieldsType fieldsType){
		List<String> valueList = queryResult.getValues();
		List<FieldsType> fieldTypeList = queryResult.getFieldTypes();
		
		for (int i = 0; i < fieldTypeList.size(); i++) {
			if(fieldTypeList.get(i)==fieldsType){
				return valueList.get(i);
			}
		}
		return null;
	}
	
	/**
	 * 只保留MONITOR类型的fieldName与values的值
	 * @param queryResult
	 */
	private void retainMonitorQueryResult(QueryResult queryResult){
		
		List<String> fieldNames = queryResult.getFieldNames();
		List<String> values = queryResult.getValues();
		for (int i = queryResult.getFieldTypes().size()-1; i >= 0; i--) {
			if(queryResult.getFieldTypes().get(i)!=FieldsType.MONITOR){
				fieldNames.remove(i);
				values.remove(i);
			}
		}
	}
	
	/**
	 * 获得监测的阈值
	 * @param efcdList
	 * @return
	 */
	private String[] getMonitorThreadsholdValue(List<EventFieldConditionDetail> efcdList){
		List<String> result = new ArrayList<String>();
		for (int i = 0; i < efcdList.size(); i++) {
			if(efcdList.get(i).getField().getType().equalsIgnoreCase(FieldsType.MONITOR.value())){
				result.add(efcdList.get(i).getEfc().getValue()+"");
			}
		}
		return (result.toArray(new String[0]));
	}
	
	
	
	
	

}
